<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Schedule extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->model('Usernum_model');
        $this->load->model('Product_model');
        //$this->load->model('Order_model');
        $this->load->model('Schedule_model');
        $this->load->model('cert_model');
    }
	

	public function index($userid)
	{
		$dbprefix = $this->User_model->db->dbprefix;

		//浏览
		$sysVisittype = _get_config('visittype');
		$this->load->service('user_service');
		$this->user_service->visit($userid, $this->loginID, $sysVisittype['home']);
		//-浏览

		$y = $this->input->post('y');
		$m = $this->input->post('m');
		$getM = $_GET['month'];
		if(empty($y)) $y = date('Y');
		if (empty($m) && !empty($getM)) 
		    $m = $getM;
		elseif (empty($m))
		    $m = date('m');

		$this->load->service('user_service');
		$oUser = $this->user_service->get_user_homeinfo($userid, $this->loginID);
        
        $insid = $oUser['insid'];

        //如果此人是属于经纪公司的，此经纪公司是认证过的，则旗下所有的人都是认证过的
        if(!empty($insid)) {
            $userCert = $this->cert_model->get_by_id($insid);
            $ins_user = $this->User_model->get_info_by_id($insid);
        } else {
            //认证
            $userCert = $this->cert_model->get_by_id($userid);
        }

		$oSysItem = _get_config('workitem');
		$oSysScene = _get_config('workscene');
		$oSysTime = _get_config('worktime');
		$item_label = '工作内容';
		$scene_label = '工作场景';
		
		if ($oUser['usertype'] == 6)
		{
			$this->load->model('Userdetail_model');
			$userdetail = $this->Userdetail_model->get_info_by_userid($userid);
			if ($userdetail)
			{
				if ($userdetail['type'])
				{
					$artist_conf = $this->config->item('artist');
					$oSysItem = $artist_conf[$userdetail['type']]['workitem'];
					$oSysScene = $artist_conf[$userdetail['type']]['workscene'];
					$oSysTime = $artist_conf[$userdetail['type']]['worktime'];
					$item_label = $artist_conf[$userdetail['type']]['item_label'];
					$scene_label = $artist_conf[$userdetail['type']]['scene_label'];
				}
			}
		}
		else if (in_array($oUser['usertype'], array(4,5)))
		{
			$type = $oUser['usertype']==4?1:2;
			$artist_conf = $this->config->item('artist');
			$workitem = $artist_conf[$type]['workitem'];
			$workscene = $artist_conf[$type]['workscene'];
			$worktime = $artist_conf[$type]['worktime'];
			$item_label = $artist_conf[$type]['item_label'];
			$scene_label = $artist_conf[$type]['scene_label'];
		}
			
		
		
		$oProduList = $this->Product_model->get_product_by_uid($userid);
		$oProductList = array();
		$oItemList = array();
		$oItem_Scene_Time = array();
		$oSceneList = array();
		$oTimeList = array();
		//工作形式
		$oShoottypeList = array();
		//工作备注
		$workRemark = array();
		//内衣泳装加收百分比
		$addPercentage = array();
		//工作形式动态选择(key为三者叠加)
		$shootList = array();
		if($oProduList)
		{
			
			foreach ($oProduList as $key => $a) {
                
                if (!isset($oSysItem[$a['item']])) {
                    continue;
                }
                //模特工作备注
                if ($oUser['usertype'] == 1)
                {
                    //拍摄类型
                    $remarkText = "";
                    //加收百分比
                    $percentage = 0;
                    //工作形式
                    $shootArr = array();
                    if ($a['shoottype'] == 1)
                    {
//                         $remarkText .= '款式拍摄'.$a['shootpic'].'件以内';
                        //工作形式
                        $shootpic = $a['shootpic']?$a['shootpic']:'不限';
                        $oShoottypeList[$a['shoottype']] = '款式拍摄'.$shootpic.'件起拍';
                        $shootArr['key'] = $a['shoottype'];
                        $shootArr['value'] = '款式拍摄'.$shootpic.'件起拍';
                    }
                    if ($a['shoottype'] == 2)
                    {
//                         $remarkText .= '形象拍摄'.$a['shootpic'].'件以内';
                        //工作形式
                        $shootpic = $a['shootpic']?$a['shootpic']:'不限';
                        $oShoottypeList[$a['shoottype']] = '形象拍摄'.$shootpic.'件起拍';
                        $shootArr['key'] = $a['shoottype'];
                        $shootArr['value'] = '形象拍摄'.$shootpic.'件起拍';
                    }
                    if ($a['shoottype'] == 3)
                    {
//                         $remarkText .= '不限';
                        //工作形式
                        $oShoottypeList[$a['shoottype']] = '不限';
                        $shootArr['key'] = $a['shoottype'];
                        $shootArr['value'] = '不限';
                    }
                    //内衣泳装
                    if ($a['swimsuit'] == 0.1)
                    {
                        if ($remarkText)
                            $remarkText .= '，';
                        $remarkText .= '不接内衣泳装';
                    }
                    if ($a['swimsuit'] > 0.1)
                    {
                        if ($remarkText)
                            $remarkText .= '，';
                        $remarkText .= '内衣泳装加收'.$a['swimsuit'].'%';
                        $percentage += $a['swimsuit'];
                    }
                    //情趣内衣
                    if ($a['appealsuit'] == 0.1)
                    {
                        if ($remarkText)
                            $remarkText .= '，';
                        $remarkText .= '不接情趣内衣';
                    }
                    if ($a['appealsuit'] > 0.1)
                    {
                        if ($remarkText)
                            $remarkText .= '，';
                        $remarkText .= '情趣内衣加收'.$a['appealsuit'].'%';
                        $percentage += $a['appealsuit'];
                    }
                    if ($remarkText)
                        $remarkText .= '，';
                    $remarkText .= '外地工作价格加20%，不含来回车旅住宿费，具体费用实报实销。';
                    //模特本地下单几小时起拍，外地下单几小时起拍备注
                    $this->load->model('Userdetail_model');
                    $userdetail = $this->Userdetail_model->get_info_by_userid($userid);
                    if ($userdetail['local_order'])
                        $remarkText .= '本地工作'.$userdetail['local_order'].'小时起拍。';
                    if ($userdetail['field_order'])
                        $remarkText .= '外地工作'.$userdetail['field_order'].'小时起拍。';
                    
                    $workRemark[ $a['item'] .'_'. $a['scene'] .'_'. $a['time'].'_'.$a['shoottype']] = $remarkText;
//                     $addPercentage[$a['item'] .'_'. $a['scene'] .'_'. $a['time']] = $percentage;
                    
                    $oProductList[ $a['item'] .'_'. $a['scene'] .'_'. $a['time'].'_'.$a['shoottype'] ] = $a['price'];
                
                    //工作形式
                    if (!array_key_exists($a['item'].'_'.$a['scene'].'_'.$a['time'], $shootList))
                    {
                        $shootList[$a['item'].'_'.$a['scene'].'_'.$a['time']] = array();
                    }
                    array_push($shootList[$a['item'].'_'.$a['scene'].'_'.$a['time']], $shootArr);
                }
                else 
                {
                    $oProductList[ $a['item'] .'_'. $a['scene'] .'_'. $a['time'] ] = $a['price'];
                }
				$oItemList[$a['item']] = $oSysItem[$a['item']];

				$thatScene = $a['scene']?$oSysScene[$a['scene']]:'';
				$thatTime = $oSysTime[$a['time']];
				if(key($oItemList)==$a['item'] )
				{
					if ($a['scene'])
					{
						$oSceneList[$a['scene']] = $thatScene;
					}
// 					if(key($oSceneList)==$a['scene'])
					$oTimeList[$a['time']] = $thatTime;
				}

				if(!array_key_exists($a['item'], $oItem_Scene_Time))
					$oItem_Scene_Time[$a['item']] = array( array('scene_key'=>$a['scene'],'scene_name'=>$thatScene,'time_list'=>array($a['time']=>$thatTime)) );
				else{
					/*

					if(!in_array($a['scene'], $oItem_Scene_Time[$a['item']]['scene_key']))
					{
						array_push($oItem_Scene_Time[$a['item']], array('scene_key'=>$a['scene'],'scene_name'=>$thatScene,'time_list'=>array($a['time']=>$thatTime)) );
					}
					else
					{
						$oItem_Scene_Time[$a['item']]['time_list'][$a['time']]=$thatTime;
					}
					*/

					
					$bExist = false;
					foreach ($oItem_Scene_Time[$a['item']] as $key => $av) {
						if($av['scene_key']==$a['scene'])
						{
							$oItem_Scene_Time[$a['item']][$key]['time_list'][$a['time']] = $thatTime;
							$bExist = true;
							break;
						}
					}
					if(!$bExist)
					{
						array_push($oItem_Scene_Time[$a['item']], array('scene_key'=>$a['scene'],'scene_name'=>$thatScene,'time_list'=>array($a['time']=>$thatTime)) );
					}
					
				}
			}

		}
		else
		{
			$oItemList = $oSysItem;
			$oSceneList = $oSysScene;
			$oTimeList = $oSysTime;
		}
		//默认汇总
		//$this->load->helper('site');
		//$oDefaultPrice = _get_product_default_price();
		//$oProductList = array_merge($oDefaultPrice, $oProductList);


		// $list = array(
		// 	array('id'=>1,'datetime'=>'2015-05-01 8:30:00','title'=>'周末特色文化广场活动吧','yinyueji'=>1,'classid'=>1),
		// 	array('id'=>11,'datetime'=>'2015-05-11 10:00:03','title'=>'周末特色文化广场活动','yinyueji'=>1,'classid'=>1),
		// 	array('id'=>2,'datetime'=>'2015-05-9 16:00:00','title'=>'test','yinyueji'=>0,'classid'=>1),
		// 	array('id'=>3,'datetime'=>'2015-05-1 18:00:00','title'=>'kk','yinyueji'=>1,'classid'=>12),
		// 	array('id'=>5,'datetime'=>'2015-05-15 9:20:20','title'=>'测试','yinyueji'=>1,'classid'=>1),
		// 	);

		/* //订单->档期
		$oSysPaystatus = $this->config->item('get_paystatus');
		$arrWhere = array('a.sellerid'=>$userid,
				'a.status'=>1,'paystatus'=>"'".$oSysPaystatus[3]."'");
		$tb = $dbprefix.'order a left join '.$dbprefix.'order_book b on(a.id=b.orderid)';
		$list_result = $this->Order_model->fetch_page(1, 100, $arrWhere,'a.id,a.title,b.begtime,b.endtime','a.addtime desc',$tb);

		$list = array();
		foreach ($list_result['rows'] as $key => $a) {
			$list[] = array('id'=>$a['id'],'datetime'=>$a['begtime'],'endtime'=>$a['endtime'],'title'=>$a['title']);
			if( $a['endtime'] > $a['begtime'] && ($a['endtime']-$a['begtime'])>= 24*60*60 )
			{
				$date_i = ($a['endtime']-$a['begtime'])/(24*60*60);
				for($i=0; $i<$date_i; $i++)
				{
					$list[] = array('id'=>$a['id'],'datetime'=>$a['endtime'] - 24*60*60*$i,'endtime'=>$a['endtime'],'title'=>$a['title']);
				}

			}

		}
		*/

		//档期
		$arrWhere = array('userid'=>$userid,
				'status'=>1);
		$list = $this->Schedule_model->fetch_page(1, 100, $arrWhere,'id,dodate as datetime,enddate as endtime,thing as title,type,time_zone','addtime desc');

		
		//用户类型、分类
		$userRole = '';
		if ($oUser['usertype'] == 1)
		{
			$userRole = $oUser['role']?$oUser['role']:'模特';
		}
		elseif ($oUser['usertype'] == 2)
		{
			$oSysType = _get_config('type');
			$oSysType = $oSysType[2];
			$oSysStr = '';
			foreach ($oSysType as $key => $v)
			{
				if(strpos(','.$oUser['type'].',',','.$key.',')>-1)
				{
					if ($oSysStr)
					{
						$oSysStr .= ','.$v;
					}
					else
					{
						$oSysStr = $v;
					}
				}
			}
			$userRole = $oSysStr?$oSysStr:'机构';
		}
		elseif ($oUser['usertype'] == 3)
		{
			$userRole = '公司';
		}
		elseif ($oUser['usertype'] == 4)
		{
			$userRole = $oUser['role']?$oUser['role']:'摄影师';
		}
		elseif ($oUser['usertype'] == 5)
		{
			$userRole = $oUser['role']?$oUser['role']:'化妆师';
		}
		elseif ($oUser['usertype'] == 6)
		{
			if ($oUser['role'])
			{
				$userRole = $oUser['role'];
			}
			else
			{
				if ($oUser['type'])
				{
					$oSysType = _get_config('type');
					$oSysType = $oSysType[6];
					$userRole = $oSysType[$oUser['type']];
				}
				else
				{
					$userRole = '艺人';
				}
			}
		}
		$result = array(
			//'o' => $o,
			'oItemList'=> $oItemList,
			'oSceneList'=> $oSceneList,
			'oTimeList'=> $oTimeList,
		    //工作形式
		    'oShoottypeList'=>$oShoottypeList,
		    'shootList' => $shootList,

			'oSysScene'=>$oSysScene,
			'oSysTime'=>$oSysTime,
			
			'oProductList' => $oProductList,
		    'workRemark'=>$workRemark,
		    'addPercentage'=>$addPercentage,
			'oItem_Scene_Time' => $oItem_Scene_Time,
			'oUser' => $oUser,
            'oCert' => $userCert,
			'list' => $list,
			'YM'=>array('y'=>$y,'m'=>$m),
			'item_label' => $item_label,
			'scene_label' => $scene_label,
			'userRole' => $userRole
			);
		if (isset($ins_user))
		{
			$result['ins_user'] = $ins_user;
		}
		$this->load->view('i/schedule',$result);
	}

	public function book()
	{
		$res = array('code'=>0, 'data'=>array());

		if ($this->input->is_post())
		{
			if(!$this->loginID)
			{
				$res['code'] = 201;
				$res['data']['error_messages'] = array('请先登录');
			}
			else{
				$this->load->service('order_service');
				$res = $this->order_service->book();
			}
		}

		header("Content-type:text/html;charset=utf-8");
		$this->view->json($res);

	}

}